import isEmpty from 'loadsh/isEmpty'
import compact from 'loadsh/compact'
import { CORPID, SPACEID } from '/constData/requestFun/_globalData';
import { GetSalaryInfo, GetSalaryInfoById, PostSalaryInfo, SalaryInfoDeleteData } from '/constData/requestFun/_teseRequest';
Page({
  data: {
    approve: [],
    item: 1,
    min: null,
    max: new Date('2033/12/02'),
    defaultDate: new Date(),
    SalaryInfo: {
      CorpId: '', //申请人名字
      StartDate: new Date(), //申请时间
      Amount:'',//金额
      MainContent: '',//理由
      Annex: '[]'//附件
    },
    displaydata: [],
    ReceiveData: [],
  },

  Delete(e) {
    const id = e.currentTarget.dataset.id
    SalaryInfoDeleteData(id, (res) => {
      my.alert({
        title: '撤销成功',
      });
      //刷新数据
      this.setData({
        ReceiveData: this.data.ReceiveData.filter((it) => {
          return it.item1.id !== id
        })
      })
    }, (err) => {
      my.alert({
        title: '撤销失败',
      });
    })
  },
  handleOk1(date, format, e) {
    this.setData({
      SalaryInfo: {
        ...this.data.SalaryInfo,
        StartDate: date,
      },
      min: date
    })
  },
  OnMainContent(value) {
    this.setData({
      SalaryInfo: {
        ...this.data.SalaryInfo,
        MainContent: value
      }
    })
  },
  OnAmount(value) {
    this.setData({
      SalaryInfo: {
        ...this.data.SalaryInfo,
        Amount: value
      }
    })
  },
  //获取数据
  onLoad() {
    my.getStorage({
      key: 'userInfo',
      success: (storage) => {
        GetSalaryInfo((res) => {
          // console.log('res',res);
          const cacheData = res.data.map(it => {
            if (it.item1.corpId === storage.data.result.userid) {
              it.item1['displayAnnex'] = JSON.parse(it.item1.annex)
              return it
            }
          })
          this.setData({
            SalaryInfo: {
              ...this.data.SalaryInfo,
              CorpId: storage.data.result.userid
            },
            ReceiveData: compact(cacheData).reverse()
          })

        })
      }
    })
  },
  // 上传数据
  onSubmit(e) {
    console.log('提交数据', JSON.stringify(this.data.SalaryInfo));
    PostSalaryInfo(JSON.stringify(this.data.SalaryInfo),
      (res) => {
        console.log('sss',res);
        my.alert({
          title: '提交成功',
        });
        GetSalaryInfoById(res.data, (res) => {
          this.setData({
            ReceiveData: compact([...this.data.ReceiveData, res.data].map(it => {
              if (it.item1.corpId === this.data.SalaryInfo.CorpId) {
                it.item1['displayAnnex'] = JSON.parse(it.item1.annex)
                return it
              }
            })).reverse()
          }) 
        })
        this.setData({
          SalaryInfo: {
            ...this.data.SalaryInfo,
            StartDate: new Date(), //申请时间
            Amount:'',//金额
            MainContent: '',//理由
            Annex: '[]'//附件
          },
        })
      },
      (err) => {
        my.alert({
          title: '提交成功1',
        });
      })
  },
  //上传图片
  onUpload() {
    let imageAnnex = ''
    dd.uploadAttachmentToDingTalk({
      image: {
        max: 9,
        spaceId: SPACEID,
        compress: true,
        folderId: '',
        multiple: true,
      },
      types: ['photo'],
      success: (res) => {
        const srcAnnex = this.data.SalaryInfo.Annex
        imageAnnex = res.data
        imageAnnex.push(...JSON.parse(srcAnnex))

        this.setData({
          displaydata: imageAnnex,
          SalaryInfo: {
            ...this.data.SalaryInfo,
            Annex: JSON.stringify(imageAnnex)
          }
        })
      },
      fail: () => { },
      complete: () => { },
    });
  },
  /**
  * 预览图片
  */
  PreviewBtn(e) {
    const item = e.currentTarget.dataset.item
    dd.previewFileInDingTalk({
      spaceId: item.spaceId,
      fileName: item.fileName,
      corpId: CORPID,
      fileSize: item.fileSize,
      fileType: item.fileType,
      fileId: item.fileId,
    });
  },
  CheckPictures(e) {
    const item = e.currentTarget.dataset.item
    dd.previewFileInDingTalk({
      spaceId: item.spaceId,
      fileName: item.fileName,
      corpId: CORPID,
      fileSize: item.fileSize,
      fileType: item.fileType,
      fileId: item.fileId,
    });
  },
  //图片下载函数
  DownImg(e) {
    const imgNames = e.currentTarget.dataset.annex.split(',');
    if (isEmpty(e.currentTarget.dataset.annex)) {
      my.alert({ content: '无附件图片！' })
    } else {
      Promise.all(imgNames.map(imgName =>
        new Promise((resolve) => {
          GetInfoPicture(imgName, (res) => {
            resolve(res.filePath)
          })
        })
      )).then(res => {
        GetPreviewImage(res)
      })
    }
  },
});

function DayCompute(StartDate, StartDateSuffix, EndDate, EndDateSuffix) {
  if (StartDateSuffix === EndDateSuffix) {
    return Math.ceil(((EndDate - StartDate) / 1000 / 60 / 60 / 24)) + 0.5;
  }
  else {
    if (StartDateSuffix === '上午' && EndDateSuffix === '下午') {
      return Math.ceil(((EndDate - StartDate) / 1000 / 60 / 60 / 24)) + 1;
    }
    if (StartDateSuffix === '下午' && EndDateSuffix === '上午') {
      return Math.ceil(((EndDate - StartDate) / 1000 / 60 / 60 / 24));
    }
  }
}